using System;
using System.Xml;
using System.IO;
using System.Collections;

using Icodeon;

using NUnit.Framework;

namespace Icodeon.Util
{
    [TestFixture]
    public class XmlUtilsTest : BaseTest
    {
        [TestFixtureSetUp]
        public void Init()
        {
            this.SetUp();
        }

        [TestFixtureTearDown]
        public void Dispose()
        {
        }

        [Test]
        public void TestGetNamedChildElement()
        {
            try
            {
                /*
                XmlUtils xmlUtils = new XmlUtils();
                XmlElement metadata = xmlUtils.GetNamedChildElement(this.manifest, "metadata");
                Assert.IsNotNull(metadata);
                XmlElement schemaversion = xmlUtils.GetNamedChildElement(metadata, "schemaversion");
                Assert.IsNotNull(schemaversion);
                String value = schemaversion.InnerText;
                Assert.IsNotNull(value);
                Assert.AreEqual("2004 3rd Edition", value);
                 */ 
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestGetNamedChildElements()
        {
            try
            {
                XmlUtils xmlUtils = new XmlUtils();
                XmlElement resources = xmlUtils.GetNamedChildElement(this.manifest, "resources");
                Assert.IsNotNull(resources);
                XmlElement[] resourceArray = xmlUtils.GetNamedChildElements(resources, "resource");
                Assert.AreEqual(1, resourceArray.Length);

            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestGetAttributeValue()
        {
            try
            {
                XmlUtils xmlUtils = new XmlUtils();
                XmlElement organizations = xmlUtils.GetNamedChildElement(this.manifest, "organizations");
                Assert.IsNotNull(organizations);
                String value = xmlUtils.GetAttributeValue(organizations, "default");
                Assert.IsNotNull(value);
                Assert.AreEqual("ORG-D2487803-024E-F6FE-7AC5-7A3C60FAADA4", value);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }

        [Test]
        public void TestSetAttributeValue()
        {
            try
            {
                XmlUtils xmlUtils = new XmlUtils();
                XmlElement organizations = xmlUtils.GetNamedChildElement(this.manifest, "organizations");
                Assert.IsNotNull(organizations);
                String value = xmlUtils.GetAttributeValue(organizations, "default");
                Assert.IsNotNull(value);

                
                String newValue = "newValue";
                bool ok = xmlUtils.SetAttributeValue(organizations, "default", newValue);
                Assert.IsTrue(ok);
                value = xmlUtils.GetAttributeValue(organizations, "default");
                Assert.IsNotNull(value);
                Assert.AreEqual(newValue, value);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                if (e.InnerException != null)
                {
                    System.Console.WriteLine(e.InnerException.Message);
                }
                System.Console.WriteLine(e.StackTrace);
                Assert.Fail(e.Message);
            }
        }
    }
}
